package cn.lds.health.controller;

import cn.lds.health.dto.ResultDTO;
import cn.lds.health.entity.HealthExamItem;
import cn.lds.health.entity.HealthExamPackage;
import cn.lds.health.exception.SysException;
import cn.lds.health.service.HealthExamItemService;
import cn.lds.health.service.HealthExamPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/exam_package")
public class HealthExamPackageController {
	
	@Autowired
	private HealthExamPackageService healthExamPackageService;
	
	@Autowired
	private HealthExamItemService healthExamItemService;
	
	@RequestMapping("/show_packages")
	public String showPackage(Model model){
		
		ResultDTO<List<HealthExamPackage>> listResultDTO = null;
		try {
			listResultDTO = healthExamPackageService.selectHealthPackageAll();
		} catch (SysException e) {
			e.printStackTrace();
		}
		model.addAttribute("result",listResultDTO);
		return "show_exam_package";
	}
	
	@RequestMapping("/show_package_list")
	public String showPackageAll(Model model){
		ResultDTO<List<HealthExamPackage>> listResultDTO = null;
		try {
			listResultDTO = healthExamPackageService.selectHealthPackageAll();
		} catch (SysException e) {
			e.printStackTrace();
		}
		model.addAttribute("result",listResultDTO);
		
		return "show_exam_package_list";
	}
	
	@RequestMapping("/show_package_desc/{id}")
	public String showPackageDesc(@PathVariable("id") Integer id,Model model){
		
		ResultDTO<HealthExamPackage> healthExamPackageResultDTO = null;
		try {
			healthExamPackageResultDTO = healthExamPackageService.selectHealthPackageById(id);
		} catch (SysException e) {
			e.printStackTrace();
		}
		
		model.addAttribute("result",healthExamPackageResultDTO);
		
		return "show_exam_package_desc";
	}
	
	/**
	 * 跳转到添加套餐页面
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping("/add_health_package")
	public String addExamPackage(Model model) throws SysException {
		
		//查询所有的体检项目
		ResultDTO<List<HealthExamItem>> listResultDTO = healthExamItemService.selectHealthExamItemAll();
		//放入Request作用域
		model.addAttribute("healthExams",listResultDTO.getData());
		
		return "add_exam_package";
		
	}
	
	/**
	 * 接收新建套餐信息
	 * @param healthExamPackage
	 * @param model
	 * @param examItems
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "do_add_health_package")
	public String doAddExamPackage(HealthExamPackage healthExamPackage, Model model,Integer... examItems) throws SysException {
		
		System.out.println("aa:"+healthExamPackage);
		System.out.println("aa:"+ Arrays.toString(examItems));
		ResultDTO resultDTO = healthExamPackageService.addHealthExamPackage(healthExamPackage, examItems);
		
		model.addAttribute("result",resultDTO);
		
		return "add_success";
	}
}